package ihm.panel.admin;

import java.awt.CardLayout;
import java.awt.Component;

import javax.swing.JPanel;

import ihm.applet.KrakenApplet;
import ihm.panel.KrakenTablePanel;
import ihm.panel.PanelInterface;
import ihm.panel.TablePanel;

/**
 * Onglet d'administration des utilisateurs.
 * @author Guillaume Pocas & Jean-Fraçois Augé
 */
public class AdministrationPanel extends JPanel implements PanelInterface {
	
	private static final long serialVersionUID = 1L;
	private KrakenApplet applet;
	private JPanel list_jp;
	private JPanel add_jp;
	private UserRightsPanel right_jp;
	
	/**
	 * Constructeur.
	 * @param applet
	 */
	public AdministrationPanel(KrakenApplet applet) {
		this.applet = applet;
		initPanel();
	}

	@Override
	public void initPanel() {
		setLayout(new CardLayout());
		
		list_jp = new ListUsersPanel(applet);
		add_jp = new NewUserPanel(applet);
		right_jp = new UserRightsPanel(applet);
		
		add(list_jp, "Lister");
		add(add_jp, "Ajouter");
		add(right_jp, "Droits");
	}
	
	/**
	 * Set the main panel to the new chosen panel.
	 * @param panelName
	 */
	public void setPanel(String panelName) {
		((CardLayout) this.getLayout()).show(this, panelName);
		
		JPanel panel = getCurrentPanel();
		if(panel instanceof KrakenTablePanel) {
			if(panel.isShowing()){
				KrakenTablePanel pan = (KrakenTablePanel) panel;
				pan.updateTableInfo();
				pan.getAppletTableModel().setData(pan.getColumnContent());
				pan.getAppletTableModel().fireTableDataChanged();
			}
		}
		
		if(panel instanceof TablePanel) {
			if(panel.isShowing())
				((TablePanel) panel).updateTableInfo();
		}
	}
	
	/**
	 * Retourne le panneau d'administration.
	 * @return
	 */
	public JPanel getCurrentPanel() {
        JPanel currentPanel = null;

        for(Component component : this.getComponents()) {
        	if(component instanceof JPanel && component.isVisible()) {
                currentPanel = (JPanel) component;
            }
        }

        return currentPanel;
    }
	
	/**
	 * Retourne le panneau des droits des utilisateurs.
	 * @return
	 */
	public UserRightsPanel getRightPanel(){ return right_jp; }
	
}
